package com.jdew.thkj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jdew.thkj.R;
import com.jdew.thkj.bean.CatesBean;
import com.jdew.thkj.onitemInterface.OnItemClickListener;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yangyang on 2017/9/18.
 */

public class HomeAdapter extends RecyclerView.Adapter <RecyclerView.ViewHolder> implements View.OnClickListener {
    public List <CatesBean.DataBeanX.DataBean> dataList;
    private LayoutInflater layoutInflater;
    private Context context;
    private View itemView;
    private OnItemClickListener mItemClickListener;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private String maType;
    //正在加载中
    public static final int LOADING_MORE = 1;
    //没有加载更多 隐藏
    public static final int NO_LOAD_MORE = 2;

    //上拉加载更多状态-默认为0
    private int mLoadMoreStatus = 0;


    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick( (Integer) v.getTag() );
        }
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public HomeAdapter(Context context, List <CatesBean.DataBeanX.DataBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from( context );
        sp = context.getSharedPreferences( "user", Activity.MODE_PRIVATE );
        editor = sp.edit();
        maType = sp.getString( "SpareTwo", "" );
    }

    public void setDataList(List <CatesBean.DataBeanX.DataBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            itemView = layoutInflater.from( context ).inflate( R.layout.product_list_item, parent, false );
            itemView.setOnClickListener( this );
            return new MyViewHolder( itemView );
        } else if (viewType == TYPE_FOOTER) {
            itemView = layoutInflater.from( context ).inflate( R.layout.item_footer, parent,
                    false );
            return new FootViewHolder( itemView );
        }
        return new MyViewHolder( itemView );
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myviewHolder = (MyViewHolder) holder;
            myviewHolder.BindView( holder, position );
            myviewHolder.itemView.setTag( position );
        } else if (holder instanceof FootViewHolder) {
            FootViewHolder footerViewHolder = (FootViewHolder) holder;
            switch (mLoadMoreStatus) {
                case LOADING_MORE:
                    footerViewHolder.mTvLoadText.setText( "正加载更多..." );
                    break;
                case NO_LOAD_MORE:
                    footerViewHolder.mTvLoadText.setText( "已全部加载" );
                    footerViewHolder.progressBar.setVisibility( View.GONE );
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size() == 0 ? 0 : dataList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.product)
        ImageView product;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.price)
        TextView price;
        @Bind(R.id.before_price)
        TextView beforePrice;
        @Bind(R.id.quan)
        TextView quan;
        @Bind(R.id.earn)
        TextView earn;
        @Bind(R.id.num)
        TextView num;
        @Bind(R.id.source)
        ImageView source;

        MyViewHolder(View itemView) {
            super( itemView );
            ButterKnife.bind( this, itemView );
        }

        public void BindView(RecyclerView.ViewHolder holder, int position) {
            DecimalFormat df = new DecimalFormat( "0.00" );
            ((MyViewHolder) holder).beforePrice.getPaint().setFlags( Paint.STRIKE_THRU_TEXT_FLAG );
            if (dataList.get( position ).getIs_tmall().equals( "1" )) {
                source.setImageResource( R.mipmap.tm );
            } else {
                source.setImageResource( R.mipmap.tb );
            }
            String quan = dataList.get( position ).getCoupon_amount().replace( ".0", "" );
            if (Double.parseDouble( quan ) > Double.parseDouble( dataList.get( position ).getGoods_price() )) {
                quan = "0";
            } else {
                quan = dataList.get( position ).getCoupon_amount().replace( ".0", "" );
            }
            ((MyViewHolder) holder).beforePrice.setText( "原价" + df.format( Double.parseDouble( dataList.get( position ).getGoods_price() ) ) );
            ((MyViewHolder) holder).name.setText( dataList.get( position ).getGoods_title() );
            ((MyViewHolder) holder).num.setText( "月销:" + dataList.get( position ).getGoods_sale_num() );
            ((MyViewHolder) holder).price.setText( df.format( Double.parseDouble( dataList.get( position ).getGoods_price() ) - Double.parseDouble( quan ) ) );
            Double high = Double.parseDouble( dataList.get( position ).getCommission_rate() );
            ((MyViewHolder) holder).quan.setText( "券  "+quan + "元" );
            if (maType.equals( "" ) || maType.equals( "1" )) {
                earn.setVisibility( View.GONE );
                SpannableString spannableString = new SpannableString( price.getText().toString() );
                RelativeSizeSpan sizeSpan = new RelativeSizeSpan( 1.2f );
                spannableString.setSpan( sizeSpan, 0, price.getText().toString().indexOf( "." ), Spanned.SPAN_INCLUSIVE_EXCLUSIVE );
                price.setText( spannableString );
            } else {
                earn.setVisibility( View.VISIBLE );
                if (maType.equals( "2" )) {
                    ((MyViewHolder) holder).earn.setText( "赚  "+df.format( Double.parseDouble( price.getText().toString() ) * high / 100 / 2 ) );
                } else {
                    ((MyViewHolder) holder).earn.setText( "赚  "+df.format( Double.parseDouble( price.getText().toString() ) * high / 100 ) );
                }
                SpannableString spannableString = new SpannableString( price.getText().toString() );
                RelativeSizeSpan sizeSpan = new RelativeSizeSpan( 1.2f );
                spannableString.setSpan( sizeSpan, 0, price.getText().toString().indexOf( "." ), Spanned.SPAN_INCLUSIVE_EXCLUSIVE );
                price.setText( spannableString );
            }
            Glide.with( context ).load( dataList.get( position ).getGoods_pic() ).into( product );
        }
    }

    public static class FootViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.loading)
        TextView mTvLoadText;
        @Bind(R.id.progressBar)
        ProgressBar progressBar;

        public FootViewHolder(View view) {
            super( view );
            ButterKnife.bind( this, itemView );
        }
    }
    /**
     * 更新加载更多状态
     *
     * @param status
     */
    public void changeMoreStatus(int status) {
        mLoadMoreStatus = status;
        notifyDataSetChanged();
    }
}
